<!-- 课后同学们做一个电子时钟，显示当前的年月日，时分秒，要求自动变化 -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>电子时钟</title>
    <style>
        body {
            text-align: center;
            margin-top: 50px;
        }
        #clock {
            font-size: 24px;
        }
    </style>
    <script>
        function updateClock() {
            const now = new Date();
            const year = now.getFullYear();
            const month = String(now.getMonth() + 1).padStart(2, '0');
            const day = String(now.getDate()).padStart(2, '0');
            const hours = String(now.getHours()).padStart(2, '0');
            const minutes = String(now.getMinutes()).padStart(2, '0');
            const seconds = String(now.getSeconds()).padStart(2, '0');

            const clockDisplay = document.getElementById('clock');
            if (clockDisplay) {
                clockDisplay.textContent = `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
            }
        }

        // 确保DOM加载完成后初始化时钟
        window.onload = function() {
            updateClock();
            setInterval(updateClock, 1000);
        };
    </script>
</head>
<body>
    <div id="clock"></div>
</body>
</html>